package org.example.utils;

import cn.hutool.core.io.IoUtil;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @Author lyz
 * @Date 2025/3/11 10:21
 */
public class CrackJavaScript {

    /**
     * 给HttpGet设置一些必要的header
     *
     * @param get           通过get方法访问pdf资源
     */
    public static void setHeader(HttpGet get) {
        get.setHeader("Upgrade-Insecure-Requests", "1");
        get.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36");
    }

    /**
     * 将HttpResponse输出到文件, 即将pdf输入流写到硬盘.
     *
     * @param response      http响应
     * @param file          落地文件
     * @throws IOException  IO异常
     */
    public static void output(HttpResponse response, File file) throws IOException {
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        fileOutputStream.write(getResponseBodyAsBytes(response));
        fileOutputStream.flush();
        fileOutputStream.close();
    }

    /**
     * 通过破解动态JavaScript脚本,
     * 获取cookie名为 __jsl_clearance的值
     *
     * @param body          相应内容(一般为第一次请求获取到的动态js字符串)
     * @return              cookie名为 __jsl_clearance的值
     */
    public static String getJslClearance(String body) {

        String jsl_clearance = "";
        ScriptEngineManager manager = new ScriptEngineManager();
        //得到脚本引擎
        ScriptEngine engine = manager.getEngineByName("JavaScript");
        //处理加密js
        String js = body.trim().replace("<script>", "")
                .replace("</script>", "")
                .replace("document.cookie=", "")
                .replace("location.href=location.pathname+location.search", "");
        try {
            //得到解密后的js
            String result = (String) engine.eval(js);
            jsl_clearance = result.split(";")[0];
        } catch (ScriptException e) {
            e.printStackTrace();
        }
        return jsl_clearance;
    }

    /**
     * 将HTTP响应体转换为字符串返回
     *
     * @param response      HTTP响应
     * @return              响应体的字符串形式
     * @throws IOException  IO异常
     */
    public static String getResponseBodyAsString(HttpResponse response) throws IOException {
        return IoUtil.read(response.getEntity().getContent(), "UTF-8");
    }

    /**
     * 将HTTP响应体转换为byte数组返回
     *
     * @param response      HTTP响应
     * @return              响应体的byte数组形式
     * @throws IOException  IO异常
     */
    public static byte[] getResponseBodyAsBytes(HttpResponse response) throws IOException {
        return IoUtil.readBytes(response.getEntity().getContent());
    }

    /**
     * 通过响应头的set-cookie
     * 获取cookie名称为__jsluid的值
     * @param response      HttpResponse
     * @return              __jsluid的值
     */
    public static String getJsluid(HttpResponse response) {
        Header header = response.getFirstHeader("set-cookie");
        String[] split = header.getValue().split(";");
        for (String s : split) {
            if (s.contains("__jsluid")) {
                return s.trim();
            }
        }
        return "";
    }
}

